<template>
    <div>
        <s-search-bar>
            <el-form :model="rfidEquipment" label-width="110px">
                <el-form-item label="小区" prop="orgId" v-hasOrg>
                    <org-select v-model="rfidEquipment.orgId" @select="query" default-value></org-select>
                </el-form-item>
                <!-- <el-form-item prop="type" label="设备类型">
                    <equipment-type v-model="rfidEquipment.type" @select="query" :showAll="false" default-value></equipment-type>
                </el-form-item> -->
                <el-form-item prop="equipmentName" label="设备名称">
                    <el-input v-model="rfidEquipment.equipmentName"></el-input>
                </el-form-item>
                <el-button @click="query" icon="el-icon-search" style="margin-left:20px" type="primary">查询</el-button>
            </el-form>
        </s-search-bar>

        <basic-table :tableLoading="tableLoading" :tableData="tableData" :tableOption="tableOption" :tablePage="tablePage" @table-search="query">
            <template slot="isAlarmName" slot-scope="scope">
                <el-tag type="warning">{{scope.scope.row.isAlarmName}}</el-tag>
            </template>
            <template slot="type" slot-scope="scope">
                <el-tag type="success">{{_formatEquipmentType(scope.scope.row.type)}}</el-tag>
            </template>
            <template slot="isError" slot-scope="scope">
                <el-tag type="success">{{scope.scope.row.isError}}</el-tag>
            </template>

            
            <template slot="operation" slot-scope="scope">
                <el-button type="text" icon="el-icon-finished" @click="falsePositives(scope.scope.row,scope.scope.$index)">误报</el-button>
            </template>
        </basic-table>
      
    </div>
</template>


<script>
//RFID设备管理
import { tableOption} from '@/const/crud/business/rfid-record'
import * as API from '@/api/business/rfid-record'
const fn = (type)=>{
    let index = tableOption.alarmDicts.findIndex(item=>{
        return item.type == type
    })
    return index>-1?tableOption.alarmDicts[index].label:'~'
}
export default {
    data() {
        return {
            tableData: [],
            tableOption: tableOption,
            tableLoading: false,
            rfidShowDialog: false,
            videoSrc:'',
            videoTitle:'',
            tablePage: {
                size: 50,
                current: 1,
                total: 0
            },
            rfidEquipment: {
                rfidEquipmentId: '',
                equipmentName: '',
                rfidEquipmentCode: '',
                orgId: this.$store.state.data.globalOrg.id,
                rfidPower: '',
                type:'0'
            },
            captureUrl:'',
            imgDialogVisible: false,
            dialogVisible:false
        }
    },
    methods: {
        query() {
            this.tableLoading = true
            API.page(Object.assign(this.tablePage, this.rfidEquipment)).then(res => {
                this.tableLoading = false
                if (res.data.code === 0) {
                    var result = res.data.data.records
                    result.map(item => {
                        item.createTime = this.dateFormat(item.createTime)
                        item.rfidPower = item.rfidPower //+"dBp"
                        item.isError = item.isError == 0 ? '否' : '是'
                        item.isAlarmName = item.isAlarm == 0 ? '否' : '是'
                        item.cameraCaptureUrl = item.cameraCaptureUrl||''
                        item.alarmType = fn(item.alarmName)
                        item.showVideo = item.alarmName == 'videoMotion'||item.alarmName == 'nonMotorEntrying'||item.alarmName == 'custom'
                    })
                    this.tableData = result
                    this.tablePage.total = Number.parseInt(res.data.data.total)
                }
            })
        },

        dateFormat(data) {
            var dat = new Date(data).toJSON()
            var rDate = new Date(+new Date(dat) + 8 * 3600 * 1000)
                .toISOString()
                .replace(/T/g, ' ')
                .replace(/\.[\d]{3}Z/, '')
            return rDate
        },
        rowDel(row) {
            this.$confirm('是否删除当前数据?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                API.deleteById(row.id).then(res => {
                    if(res.data.code === 0){
                        this.$message.success('删除成功')
                        this.query()
                    }
                })
            })
        },
        falsePositives(row){
            this.$confirm('当前报警数据是否误报?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                API.dealAlarm({ id: row.id,isError:1 }).then(res => {
                    if (res.data.code === 0) {
                        this.$message.success('数据误报标记成功！')
                        this.query()
                    }
                })
            })
        }
    },
    mounted() {
        this.query()
    }
}
</script>
